/*Diese Funktion reagiert auf Tastendruckevents und
  führt durch diese entsprechende Werte-Aenderungen
  im Spiel durch
*/

#include <SDL/SDL.h>
#include <stdlib.h>

unsigned int getpixelkeys(SDL_Surface *s, int x, int y)
{
  return ((unsigned int*)s->pixels)[y*(s->pitch/sizeof(unsigned int))+x];
}

int keyhandler(SDL_Rect obj[],SDL_Surface *screen,int *done,int *mode,int *autoaim,int *barspeed)
{
  Uint8 *keys;
  keys = SDL_GetKeyState(NULL);
  if(keys[SDLK_ESCAPE]){*done=1;return 0;} 
  switch(*mode)
    { 
    case 1 :
      if (keys[SDLK_s])
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y+obj[0].h+*barspeed))==16777215)
	    {
	      obj[0].y+=*barspeed;
	      return 0;
	    }
	}     
      if (keys[SDLK_w]) 
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y-*barspeed))==16777215)
	    {
	      obj[0].y-=*barspeed;
	      return 0;
	    }
	}
      if (keys[SDLK_a])
	{
	  switch (*autoaim)
	    {
	    case 0 : *autoaim=1;break;
	    case 1 : *autoaim=0;break;
	    }
	  SDL_Delay(40);
	  return 0;
	}
      if(keys[SDLK_SPACE]) 
	{
	  return 1;
	}
      break;     
    case 2 :
      if (keys[SDLK_s])
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y+obj[0].h+*barspeed))==16777215)
	    {
	      SDL_FillRect(screen,&obj[0],SDL_MapRGB(screen->format,255,255,255));
	      obj[0].y+=*barspeed;
	      return 0;
	    }
	}     
      if (keys[SDLK_w]) 
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y-*barspeed))==16777215)
	    {
	      obj[0].y-=*barspeed;
	      return 0;
	    }
	} 
      if (keys[SDLK_DOWN])
	{
	  if ((getpixelkeys(screen,obj[1].x,obj[1].y+obj[1].h+*barspeed))==16777215)
	    {
	      obj[1].y+=*barspeed;
	      return 0;
	    }
	}
      if (keys[SDLK_UP]) 
	{
	  if ((getpixelkeys(screen,obj[1].x,obj[1].y-*barspeed))==16777215)
	    {
	      obj[1].y-=*barspeed;
	      return 0;
	    }
	}
      if(keys[SDLK_SPACE]) 
	{
	  return 1;
	}
      break;
    case 3 :
      if (keys[SDLK_s])
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y+obj[0].h+*barspeed))==16777215)
	    {
	      obj[0].y+=*barspeed;
	      return 0;
	    }
	}     
      if (keys[SDLK_w]) 
	{
	  if ((getpixelkeys(screen,obj[0].x,obj[0].y-*barspeed))==16777215)
	    {
	      obj[0].y-=*barspeed;
	      return 0;
	    }
	}
      if(keys[SDLK_SPACE]) 
	{
	  return 1;
	}
      if (keys[SDLK_z])
	{
	  return -1;
	  break;	
	} 
    }   
  return 0;
}

